/* 1
var book = {
    title: "The Principles of Object-Oriented JavaScript"
};

var prototype = Object.getPrototypeOf(book);

console.log(prototype === Object.prototype);
 */
/* 2
var now = new Date();
var earlier = new Date(2015, 11, 4);

console.log( now - earlier);
 */
/* 3
var book = {
	title: "The Principles of Object-Oriented JavaScript",
	toString: function() {
		return "[Book " + this.title + "]"
	}
};

var message = "Book = " + book;
console.log(message);
 */
/* 4
Object.prototype.add = function(value) {
	return this + value;
};

var book = {
    title: "The Principles of Object-Oriented JavaScript"
};

console.log(book.add(5));
console.log("title".add("end"));

var empty = {};

for (var property in empty) {
	console.log(property);
	if (empty.hasOwnProperty(property)) {
		console.log(property);
	}
};

console.log(documnt.add(true));
console.log(window.add(5));
 */
/* 5
var book = {
    title: "The Principles of Object-Oriented JavaScript"
};

// is the same as
var book = Object.create(Object.prototype, {
	title: {
		cofigurable: true,
		enumerable: true,
	    	value: "The Principles of Object-Oriented JavaScript",
		writable: true
	}
});

var person1 = {
	name: "Gump Yang",
	sayName: function() {
		console.log(this.name);
	}
};

var person2 = Object.create(person1, {
	name: {
		configurable: true,
		enumerable: true,
		value: "Cheng",
		writable: true
	}
});

person1.sayName();
person2.sayName();

console.log(person1.hasOwnProperty("sayName"));
console.log(person1.isPrototypeOf(person2));
console.log(person2.hasOwnProperty("sayName"));
 */
/* 6
var nakedObject = Object.create(null);

console.log("toString" in nakedObject);
console.log("ValueOf" in nakedObject);
 */
/* 7
function Rectangle(length, width) {
	this.length = length;
	this.width = width;
}

Rectangle.prototype.getArea = function() {
	return this.length * this.width;
};

Rectangle.prototype.toString = function() {
	return "[Rectangle " + this.length+ "x" + this.width + "]";
};

// interith from Rectangle
function Square(size) {
	this.length = size;
	this.width = size;
}

Square.prototype = new Rectangle();
Square.prototype.constructor = Square;

Square.prototype = Object.create(Rectangle.prototype, {
	constructor: {
		configurable: true,
		enumerable: true,
		value: Square,
		writable: true
	}
});

Square.prototype.toString = function() {
	return "[Square " + this.length + "x" + this.width + "]";
};

var rect = new Rectangle(5, 10);
var square = new Square(6);

console.log(rect.getArea());
console.log(square.getArea());

console.log(rect.toString());
console.log(square.toString());

console.log(rect instanceof Rectangle);
console.log(rect instanceof Object);

console.log(square instanceof Square);
console.log(square instanceof Rectangle);
console.log(square instanceof Object);
 */
/* 8
function Rectangle(length, width) {
	this.length = length;
	this.width = width;
}

Rectangle.prototype.getArea = function() {
	return this.length * this.width;
};

Rectangle.prototype.toString = function() {
	return "[Rectangle " + this.length+ "x" + this.width + "]";
};

// interith from Rectangle
function Square(size) {
	Rectangle.call(this, size, size);
}

Square.prototype = Object.create(Rectangle.prototype, {
	constructor: {
		configurable: true,
		enumerable: true,
		value: Square,
		writable: true
	}
});

Square.prototype.toString = function() {
	return "[Square " + this.length + "x" + this.width + "]";
};

var square = new Square(6);

console.log(square.length);
console.log(square.width);
console.log(square.getArea());
 */
/* 9
 */
function Rectangle(length, width) {
	this.length = length;
	this.width = width;
}

Rectangle.prototype.getArea = function() {
	return this.length * this.width;
};

Rectangle.prototype.toString = function() {
	return "[Rectangle " + this.length+ "x" + this.width + "]";
};

// interith from Rectangle
function Square(size) {
	Rectangle.call(this, size, size);
}

Square.prototype = Object.create(Rectangle.prototype, {
	constructor: {
		configurable: true,
		enumerable: true,
		value: Square,
		writable: true
	}
});

Square.prototype.toString = function() {
	var text = Rectangle.prototype.toString.call(this);
	return text.replace("Rectangle", "Square");
};

var square = new Square(6);

console.log(square.length);
console.log(square.width);
console.log(square.getArea());
console.log(square.toString());
